﻿package plugin.newguanwei
{
    import cmd.*;
    import com.core.*;
    import com.core.destroy.*;
    import com.greensock.*;
    import com.sh.game.display.*;
    import com.sh.game.global.*;
    import com.sh.game.loader.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.system.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import pluginfw.nio.*;
    import uiresource.guanzhi.*;
    import view.message.*;
    import view.mornExtends.*;
    import view.uilt.*;
    import vo.*;

    public class YupeiCtrl extends YupeiUI
    {
        private var cfgjade:Array;
        private var cfgitem:Object;
        private var page:int;
        private var currPage:int = 1;
        private var listarr:Array;
        private var cfggw:Array;
        private var time:uint = 0;
        private var time2:uint = 0;
        private var yupeiID:int = 0;
        private var ypMC:XMLMovieClip;
        private var moveNum:Number;
        private var currIndex:int;
        public var cgMC:XMLMovieClip;
        private var loader:MovieResLoader;
        private var tween:TweenMax;
        public static const chenggong:Class = YupeiCtrl_chenggong;

        public function YupeiCtrl()
        {
            return;
        }// end function

        override protected function initializeView() : void
        {
            var _loc_1:Sprite = null;
            super.initializeView();
            _loc_1 = new Sprite();
            _loc_1.graphics.beginFill(16777215, 1);
            _loc_1.graphics.drawRect(0, 0, 100, 368);
            _loc_1.graphics.endFill();
            _loc_1.x = 20;
            _loc_1.y = 107;
            this.addChild(_loc_1);
            list.mask = _loc_1;
            this.cfgjade = ConfigDictionary.data.jade;
            this.cfgitem = ConfigDictionary.data.item;
            this.cfggw = ConfigDictionary.data.guanwei;
            list.selectedIndex = -1;
            this.initshow();
            this.addEvent();
            return;
        }// end function

        private function completeFunc1() : void
        {
            this.cgMC = XMLMcManager.instance.getPluginMc("guanwei", "chenggong");
            if (this.cgMC)
            {
                this.cgMC.mouseChildren = false;
                this.cgMC.mouseEnabled = false;
                this.cgMC.x = 80;
                this.cgMC.y = -100;
                this.cgMC.visible = false;
                this.cgMC.play();
                this.addChild(this.cgMC);
            }
            return;
        }// end function

        public function shengjinguanwei() : void
        {
            if (this.cgMC)
            {
                this.cgMC.visible = true;
                this.cgMC.playFromTo(-1, (this.cgMC.totalFrame - 1), new Handler(this.doEnd1));
            }
            return;
        }// end function

        private function doEnd1() : void
        {
            this.cgMC.visible = false;
            this.cgMC.stop();
            return;
        }// end function

        private function loadswf(param1:String) : void
        {
            if (this.loader)
            {
                this.loader.dispose();
                this.loader = null;
            }
            this.loader = new MovieResLoader();
            this.loader.load(Config.getUrl("assets/images/mainui/yupei" + param1 + ".swf"), 2, this.mcLoaded, 0, [param1], false);
            return;
        }// end function

        private function mcLoaded(param1:String, param2:ApplicationDomain) : void
        {
            var _loc_3:XML = null;
            if (param2 != null)
            {
                _loc_3 = new XML(YupeiMC.dicypMC[param1]);
                this.ypMC = new XMLMovieClip("png.yupei" + param1, _loc_3, null, true, param2);
                this.addChildAt(this.ypMC, 1);
                this.ypMC.play();
                this.ypMC.x = 190;
                this.ypMC.y = 100;
            }
            return;
        }// end function

        public function initshow() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Object = null;
            list.renderHandler = new Handler(this.listHandler);
            this.listarr = [];
            for each (_loc_1 in this.cfgjade)
            {
                
                if (_loc_1.id)
                {
                    _loc_2 = ItemObject.create(this.cfgitem[_loc_1.id]);
                    if (_loc_2)
                    {
                        this.listarr.push(_loc_2);
                    }
                }
            }
            list.selectedIndex = -1;
            list.array = this.listarr;
            this.setBtn();
            return;
        }// end function

        public function openRefresh() : void
        {
            var _loc_1:* = UserData.getUserData("guanzhi");
            this.openSelect(_loc_1);
            return;
        }// end function

        public function openSelect(param1:int) : void
        {
            var _loc_2:int = 0;
            if (this.cgMC == null)
            {
                XMLMcManager.instance.complatefuncs["guanwei"] = this.completeFunc1;
            }
            list.selectedIndex = param1 / 3 - 1;
            if (param1 / 3 <= 0)
            {
                _loc_2 = 0;
            }
            else
            {
                _loc_2 = param1 / 3 - 1;
            }
            if (_loc_2 < 4)
            {
                list.y = 107 - 90 * _loc_2;
            }
            else if (_loc_2 >= 4 && _loc_2 < 8)
            {
                list.y = 107 - 90 * (_loc_2 - 2);
            }
            else
            {
                list.y = 107 - 90 * (_loc_2 - 3);
            }
            this.currIndex = _loc_2;
            this.propraty(_loc_2);
            return;
        }// end function

        private function moveImg() : void
        {
            if (this.ypMC)
            {
                var _loc_1:* = this.ypMC;
                var _loc_2:* = this.ypMC.y + 1;
                _loc_1.y = _loc_2;
                if (this.ypMC.y >= 110)
                {
                    this.clearTime();
                    this.setMove2();
                }
            }
            return;
        }// end function

        private function moveImg2() : void
        {
            if (this.ypMC)
            {
                var _loc_1:* = this.ypMC;
                var _loc_2:* = this.ypMC.y - 1;
                _loc_1.y = _loc_2;
                if (this.ypMC.y <= 90)
                {
                    this.clearTime2();
                    this.setMove();
                }
            }
            return;
        }// end function

        public function setMove() : void
        {
            if (this.time > 0)
            {
                clearInterval(this.time);
                this.time = 0;
            }
            this.time = setInterval(this.moveImg, 50);
            return;
        }// end function

        private function setMove2() : void
        {
            if (this.time2 > 0)
            {
                clearInterval(this.time2);
                this.time2 = 0;
            }
            this.time2 = setInterval(this.moveImg2, 50);
            return;
        }// end function

        public function clearTime() : void
        {
            if (this.time > 0)
            {
                clearInterval(this.time);
                this.time = 0;
            }
            return;
        }// end function

        public function clearTime2() : void
        {
            if (this.time2 > 0)
            {
                clearInterval(this.time2);
                this.time2 = 0;
            }
            return;
        }// end function

        private function listHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:FlashItem = null;
            var _loc_5:UrlImage = null;
            if (param2 < list.array.length)
            {
                _loc_3 = list.array[param2];
                _loc_4 = param1.getChildByName("item") as FlashItem;
                _loc_4.data = _loc_3;
                _loc_5 = param1.getChildByName("bg") as UrlImage;
                if (_loc_5)
                {
                    _loc_5.url = "cs2_guanwei/kuang.png";
                }
            }
            return;
        }// end function

        private function careerAtt(param1:int) : String
        {
            var _loc_2:String = "";
            switch(param1)
            {
                case 1:
                {
                    _loc_2 = "物理攻击：";
                    break;
                }
                case 2:
                {
                    _loc_2 = "法术攻击：";
                    break;
                }
                case 3:
                {
                    _loc_2 = "道士攻击：";
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function starNum(param1:int) : void
        {
            var _loc_2:Image = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (starBox.numChildren > 0)
            {
                DestroyUtil.removeChildren(starBox);
            }
            if (param1 < 11)
            {
                _loc_3 = 0;
                while (_loc_3 < param1)
                {
                    
                    _loc_2 = new Image();
                    _loc_2.url = "png.common.star";
                    _loc_2.x = _loc_3 * 32;
                    starBox.addChild(_loc_2);
                    _loc_3++;
                }
            }
            else
            {
                _loc_4 = 0;
                while (_loc_4 < param1 - 10)
                {
                    
                    _loc_2 = new Image();
                    _loc_2.url = "png.common.qh1";
                    _loc_2.x = _loc_4 * 32;
                    starBox.addChild(_loc_2);
                    _loc_4++;
                }
            }
            return;
        }// end function

        private function propraty(param1:int) : void
        {
            var _loc_4:int = 0;
            this.selectIndex(param1);
            this.setBtn();
            starBox.visible = false;
            if (this.cfgjade[this.listarr[param1].itemid].qhlv)
            {
                starBox.visible = true;
                this.starNum(int(this.cfgjade[this.listarr[param1].itemid].qhlv));
                _loc_4 = UserData.getUserData("career");
                att_txt.text = this.careerAtt(_loc_4);
                att.text = this.cfgjade[this.listarr[param1].itemid].armshow;
            }
            else
            {
                att_txt.text = "";
                att.text = "";
            }
            this.yupeiID = this.listarr[param1].itemid;
            shanghaiLabel.text = Number(this.listarr[param1].pkatt) / 100 + "%";
            mianshangLabel.text = Number(this.listarr[param1].pkdef) / 100 + "%";
            var _loc_2:* = UserData.getBagData();
            if (_loc_2.honor >= this.cfgjade[this.listarr[param1].itemid].honourcost)
            {
                shengwangLabel.text = this.cfgjade[this.listarr[param1].itemid].honourcost + "<font color=\'#009900\'>（已达成）</font>";
            }
            else
            {
                shengwangLabel.text = this.cfgjade[this.listarr[param1].itemid].honourcost + "<font color=\'#C72541\'>（未达成）</font>";
            }
            var _loc_3:* = UserData.getUserData("guanzhi");
            if (_loc_3 >= this.cfgjade[this.listarr[param1].itemid].limit)
            {
                nameLabel.text = this.cfggw[this.cfgjade[this.listarr[param1].itemid].limit].guanzhiname + "<font color = \'#009900\'>（已达成）</font>";
            }
            else
            {
                nameLabel.text = this.cfggw[this.cfgjade[this.listarr[param1].itemid].limit].guanzhiname + "<font color=\'#C72541\'>（未达成）</font>";
            }
            if (this.ypMC)
            {
                this.ypMC.dispose(true);
                this.removeChild(this.ypMC);
                this.ypMC = null;
            }
            this.loadswf((param1 + 1) + "");
            return;
        }// end function

        private function addEvent() : void
        {
            downBtn.addEventListener(MouseEvent.CLICK, this.up);
            upBtn.addEventListener(MouseEvent.CLICK, this.down);
            duihuanBtn.addEventListener(MouseEvent.CLICK, this.duihuan);
            shuomingLink.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            shuomingLink.addEventListener(MouseEvent.MOUSE_OUT, this.out);
            list.mouseHandler = new Handler(this.selectList);
            return;
        }// end function

        private function selectList(event:Event, param2:int) : void
        {
            var _loc_3:Image = null;
            var _loc_4:Image = null;
            if (param2 < 0 || this.currIndex == param2)
            {
                return;
            }
            if (event.type == MouseEvent.CLICK)
            {
                this.currIndex = param2;
                this.selectIndex(param2);
                this.propraty(param2);
            }
            else if (event.type == MouseEvent.ROLL_OVER)
            {
                _loc_3 = list.getCell(param2).getChildByName("fag2") as Image;
                if (_loc_3.visible)
                {
                    return;
                }
                _loc_4 = list.getCell(param2).getChildByName("fag") as Image;
                _loc_4.visible = true;
                _loc_4.disabled = true;
            }
            else if (event.type == MouseEvent.ROLL_OUT)
            {
                _loc_3 = list.getCell(param2).getChildByName("fag") as Image;
                _loc_3.visible = false;
            }
            return;
        }// end function

        private function selectIndex(param1:int) : void
        {
            var _loc_4:Box = null;
            var _loc_5:UrlImage = null;
            var _loc_6:UrlImage = null;
            var _loc_2:int = 0;
            while (_loc_2 < 9)
            {
                
                _loc_4 = list.getCell(_loc_2);
                _loc_5 = _loc_4.getChildByName("fag") as UrlImage;
                _loc_5.visible = false;
                _loc_6 = _loc_4.getChildByName("fag2") as UrlImage;
                _loc_6.visible = false;
                _loc_2++;
            }
            var _loc_3:* = list.getCell(param1).getChildByName("fag2") as UrlImage;
            _loc_3.visible = true;
            _loc_3.disabled = false;
            return;
        }// end function

        protected function out(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function onOver(event:MouseEvent) : void
        {
            var _loc_2:String = "1.官职达到相应等级，可使用荣誉值兑换对应的玉佩\n2.每次兑换的玉佩可以使用7天\n3.荣誉值可以通过在定时活动中PK其他玩家获得";
            ModelProxy.change("tips", "normal", {data:_loc_2, p:new Point()});
            return;
        }// end function

        protected function duihuan(event:MouseEvent) : void
        {
            var honor:int;
            var event:* = event;
            var guanzhi:* = UserData.getUserData("guanzhi");
            var itemid:* = this.listarr[this.currIndex].itemid;
            if (guanzhi >= this.cfgjade[itemid].limit)
            {
                honor = UserData.getBagData("honor");
                if (honor >= this.cfgjade[itemid].honourcost)
                {
                    if (this.iswear(itemid) || ItemUilt.getAllCount(itemid) > 0)
                    {
                        Alert2.show("您已经持有这个玉佩，是否再次兑换", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    duihuan1();
                }
                return;
            }// end function
            );
                    }
                    else
                    {
                        this.duihuan1();
                    }
                }
                else
                {
                    WarningManager.show(44);
                }
            }
            else
            {
                WarningManager.show(43);
            }
            return;
        }// end function

        private function duihuan1() : void
        {
            Alert2.show(" 兑换该玉佩需要消耗 " + this.cfgjade[this.yupeiID].honourcost + " 荣誉值 是否确定兑换？", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                var _loc_3:Output = null;
                if (param1 == 0)
                {
                    _loc_3 = new Output();
                    _loc_3.writeVarInt(yupeiID, false);
                    PluginManager.instance.sendToBufferServer("guanwei", CMDGuanwei.DuiYePei, _loc_3);
                    WarningManager.show(114, [cfgitem[cfgjade[yupeiID].id].name]);
                    ModelProxy.change("mainui", "logview", "失去  " + cfgjade[yupeiID].honourcost + "荣誉值");
                    shengjinguanwei();
                }
                return;
            }// end function
            );
            return;
        }// end function

        private function iswear(param1:int) : Boolean
        {
            var _loc_2:* = ModelProxy.getValue("data", "arms");
            if (_loc_2[16] && _loc_2[16].itemid == param1)
            {
                return true;
            }
            return false;
        }// end function

        protected function down(event:MouseEvent) : void
        {
            if (this.tween)
            {
                this.tween.kill();
                this.tween = null;
            }
            upBtn.disabled = true;
            list.mouseEnabled = false;
            list.mouseChildren = false;
            this.moveNum = this.moveNum + 90;
            this.tween = TweenMax.to(list, 0.5, {y:this.moveNum, onComplete:this.setBtn});
            return;
        }// end function

        protected function up(event:MouseEvent) : void
        {
            if (this.tween)
            {
                this.tween.kill();
                this.tween = null;
            }
            list.mouseEnabled = false;
            list.mouseChildren = false;
            downBtn.disabled = true;
            this.moveNum = this.moveNum - 90;
            this.tween = TweenMax.to(list, 0.5, {y:this.moveNum, onComplete:this.setBtn});
            return;
        }// end function

        private function setBtn() : void
        {
            list.mouseEnabled = true;
            list.mouseChildren = true;
            upBtn.disabled = list.y >= 107;
            downBtn.disabled = list.y <= 107 - 450;
            this.moveNum = list.y;
            return;
        }// end function

        public function closeComplete() : void
        {
            if (this.loader)
            {
                this.loader.dispose();
                this.loader = null;
            }
            if (this.ypMC)
            {
                this.ypMC.dispose(true);
                if (this.ypMC.parent)
                {
                    this.ypMC.parent.removeChild(this.ypMC);
                }
                this.ypMC = null;
            }
            if (this.cgMC)
            {
                this.cgMC.dispose();
                if (this.cgMC.parent)
                {
                    this.cgMC.parent.removeChild(this.cgMC);
                }
                this.cgMC = null;
            }
            return;
        }// end function

    }
}
